from django.db import models

# Create your models here.
# camera/models.py
from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()


class CameraTag(models.Model):
    name = models.CharField(max_length=30, unique=True)

    def __str__(self):
        return self.name


class Camera(models.Model):
    brand = models.CharField(max_length=50)                        # 品牌，如“佳能”
    model = models.CharField(max_length=100)                       # 型号
    type = models.CharField(max_length=50, default='未知')         # 类型，如“单反相机”
    release_date = models.CharField(max_length=50, default='未知') # 发布时间
    reference_price = models.CharField(max_length=50, default='未知')  # 参考价格
    image_sensor_type = models.CharField(max_length=100, default='未知')  # 图像传感器类型
    resolution = models.CharField(max_length=50, default='未知')   # 分辨率
    shutter_speed = models.CharField(max_length=50, default='未知') # 快门速度范围
    frame_rate = models.CharField(max_length=20, default='未知')   # 视频帧率
    weight = models.CharField(max_length=50, default='未知')       # 重量
    monitor_size = models.CharField(max_length=20, default='未知') # 显示屏尺寸
    bluetooth = models.CharField(max_length=10, default='未知')    # 蓝牙支持
    video = models.CharField(max_length=100, default='未知')       # 视频录制能力
    format = models.CharField(max_length=50, default='未知')       # 画幅类型（全画幅、APS-C 等）

    image_url = models.URLField(blank=True, null=True, default='未知')   # 图片链接
    tags = models.ManyToManyField(CameraTag, through='CameraCameraTag', related_name='cameras')
    created_at = models.DateTimeField(auto_now_add=True, null=True)

    def __str__(self):
        return f"{self.brand} {self.model}"

class CameraImage(models.Model):
    camera = models.ForeignKey(Camera, related_name='images', on_delete=models.CASCADE)
    image_url = models.URLField(blank=True, null=True, default='未知')

class CameraCameraTag(models.Model):
    camera = models.ForeignKey(Camera, on_delete=models.CASCADE)
    tag = models.ForeignKey(CameraTag, on_delete=models.CASCADE)

    class Meta:
        unique_together = ('camera', 'tag')


class CameraRating(models.Model):
    camera = models.ForeignKey(Camera, related_name='ratings', on_delete=models.CASCADE)
    user = models.ForeignKey(User, related_name='camera_ratings', on_delete=models.CASCADE)
    rating = models.PositiveSmallIntegerField()  # 1–5
    comment = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)


class CameraBrand(models.Model):
    name = models.CharField(max_length=50, unique=True)
    logo_url = models.URLField(blank=True, null=True)
    description = models.TextField(blank=True)

    def __str__(self):
        return self.name